package com.ft.springboottravel.controller;

import com.ft.springboottravel.bean.Shop;
import com.ft.springboottravel.service.ShopService;
import com.ft.springboottravel.service.impl.ShopServiceImpl;
import com.ft.springboottravel.util.Pager;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@RequestMapping("shop")
@Controller
public class ShopController {

    @Autowired
    private ShopServiceImpl shopServiceImpl;

    @RequestMapping("toShopList")
    public String toShopList(@Param("shopName")String shopName,@Param("shopTel") String shopTel,@Param("shopAdderss")String shopAdderss,@Param("currentPage")Integer currentPage,Model model){
        Integer index=0;
        if (currentPage!=null){
            index=currentPage;
        }
        Pager<Shop> pager = shopServiceImpl.pager(index, shopName, shopTel, shopAdderss);
        model.addAttribute("pager",pager);
        return "/shoplist";
    }

    @RequestMapping("toShopAdd")
    public String toShopAdd(){
        return "shopadd";
    }
    @RequestMapping("addShop")
    public String addShop(Shop shop){
        Shop shop1 = new Shop(3,"111","222","333");
        shopServiceImpl.AddShop(shop1);
        return "redirect:/toShopList";
    }

    @RequestMapping("toShopEdit")
    public String toShopEdit(Integer shopId ,Model model){
        Shop shop = shopServiceImpl.SelectShopId(shopId);
        model.addAttribute("shop",shop);
        return "shopedit";
    }
    @RequestMapping("editShop")
    public String editShop(Shop shop){
        shopServiceImpl.EditShop(shop);
        return "redirect:/toShopList";
    }

    @RequestMapping("delShop")
    public String delShop(Integer shopId){
        shopServiceImpl.DelShopById(shopId);
        return "redirect:/toShopList";
    }
}
